﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BOM.dao;
using BOM.entity;

namespace BOM.BaseData
{
    public partial class MaterialForm : Form
    {
        public MaterialForm()
        {
            InitializeComponent();
        }
        //保存按钮
        private void btnSave_Click(object sender, EventArgs e)
        {
            string txt = txtPrice.Text;
            Material material = new Material();
            material.setFnumber(txtNumber.Text);
            material.setFname(txtName.Text);
            material.setFgroupid(txtName.Text);
            if (txtPrice.Text.Trim() == "")
            {
                txtPrice.Text = "0";
            }
            txtPrice.Text = Math.Round(double.Parse(txtPrice.Text), 2).ToString();
            material.setFprice(txtPrice.Text);
            material.setFsize(txtSize.Text);
            material.setFunit(txtUnit.Text);
            material.setFpressarea(ckIsAreaUnit.Checked);
            material.setFpressperimeter(ckperimeter.Checked);



            if (txtPrice.Text.Trim() == "" || txtNumber.Text == "" || txtName.Text.Trim() == ""||txtName.Text.Trim()==""||ckIsAreaUnit.Checked==ckperimeter.Checked)
            {
                MessageBox.Show("编号或单价或分类编号或名字不能为空或面积单位和周长单位只能选择一个", "提示");
            }
            else
            {
                if (CustomerList.BiaoJi == 1)
                {
                    bool b = MaterialNew.UpdateMaterial(CustomerList.XuanZID);
                    if (b==true)
                    {
                        MaterialList matlist = (MaterialList)this.Owner;
                        matlist.GengXindatagridview();
                        MessageBox.Show("修改成功", "提示");
                    }
                    else
                    {
                        MessageBox.Show("修改失败", "提示");
                    }
                }
                else if(CustomerList.BiaoJi == 2)
                {
                    bool bol = MaterialNew.XinZeng(); //新增数据
                    if (bol == true)
                    {
                        MaterialList matlist = (MaterialList)this.Owner;
                        matlist.GengXindatagridview();
                        MessageBox.Show("新增成功", "提示");
                    }
                    else
                    {
                        MessageBox.Show("新增失败", "提示");
                    }
                }
            }
        }
        //修改按钮
        private void btnEdit_Click(object sender, EventArgs e)
        {
            txtNumber.ReadOnly = true;
            txtName.ReadOnly = false;
            txtPrice.ReadOnly = false;
            txtSize.ReadOnly = false;
            txtUnit.ReadOnly = false;
            txtName.ReadOnly = false;
            btnSave.Enabled = true;
            CustomerList.BiaoJi = 1;
        }
        //新增按钮
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            txtNumber.ReadOnly = false;
            txtName.ReadOnly = false;
            txtPrice.ReadOnly = false;
            txtSize.ReadOnly = false;
            txtUnit.ReadOnly = false;
            txtName.ReadOnly = false;
            txtName.Text = "";
            txtName.Text = "";
            txtNumber.Text = "";
            txtPrice.Text = "";
            txtSize.Text = "";
            txtUnit.Text = "";
            btnEdit.Enabled = false;
            btnRemove.Enabled = false;
            ckIsAreaUnit.Checked = false;
            ckperimeter.Checked = false;
            CustomerList.BiaoJi = 2;
        }
        //删除按钮
        private void btnRemove_Click(object sender, EventArgs e)
        {
            //提示用户是否删除
            DialogResult dr = MessageBox.Show("您确定要删除吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                if (txtNumber.Text == "")
                {
                    MessageBox.Show("当前没有记录", "提示");
                }
                else
                {
                    bool bol = MaterialNew.DeleteMaterial(CustomerList.XuanZID);
                    if (bol == true)
                    {
                        MaterialList matlist = (MaterialList)this.Owner;
                        matlist.GengXindatagridview();
                        MessageBox.Show("删除成功", "提示");
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示");
                    }
                    txtName.Text = "";
                    txtName.Text = "";
                    txtNumber.Text = "";
                    txtPrice.Text = "";
                    txtSize.Text = "";
                    txtUnit.Text = "";
                }
            }
        }
        //窗体的加载事件
        private void MaterialForm_Load(object sender, EventArgs e)
        {
            if (CustomerList.BiaoJi == 0)
            {
                txtNumber.ReadOnly = true; //查看状态
                txtName.ReadOnly = true;
                txtPrice.ReadOnly = true;
                txtSize.ReadOnly = true;
                txtUnit.ReadOnly = true;
                txtName.ReadOnly = true;
                btnSave.Enabled = false;
                try
                {
                    DataTable table = MaterialNew.materialIDCK(CustomerList.XuanZID);
                    txtName.Text = table.Rows[0]["fname"].ToString();
                    //MessageBox.Show(txtName.Text);
                    txtNumber.Text = table.Rows[0]["fnumber"].ToString();
                    txtPrice.Text = table.Rows[0]["fprice"].ToString();
                    txtSize.Text = table.Rows[0]["fsize"].ToString();
                    txtUnit.Text = table.Rows[0]["funit"].ToString();
                    ckIsAreaUnit.Checked = (bool)table.Rows[0]["fpressarea"];
                    ckperimeter.Checked = (bool)table.Rows[0]["fpressperimeter"];
                }
                catch
                {

                }
            }

            else if (CustomerList.BiaoJi == 1)
            {
                txtNumber.ReadOnly = false; //修改状态
                txtName.ReadOnly = false;
                txtName.ReadOnly = false;
                txtPrice.ReadOnly = false;
                txtSize.ReadOnly = false;
                txtUnit.ReadOnly = false;
                try
                {
                    DataTable table = MaterialNew.materialIDCK(CustomerList.XuanZID);
                    txtName.Text = table.Rows[0]["fname"].ToString();
                    txtNumber.Text = table.Rows[0]["fnumber"].ToString();
                    txtPrice.Text = table.Rows[0]["fprice"].ToString();
                    txtSize.Text = table.Rows[0]["fsize"].ToString();
                    txtUnit.Text = table.Rows[0]["funit"].ToString();
                    ckIsAreaUnit.Checked = (bool)table.Rows[0]["fpressarea"];
                }
                catch
                {

                }
            }
            else if (CustomerList.BiaoJi == 2)//新增状态
            {
                txtName.Text = "";
                txtNumber.Text = "";
                txtPrice.Text = "";
                txtSize.Text = "";
                txtUnit.Text = "";
                txtName.Text = "";
                btnEdit.Enabled = false;
                btnRemove.Enabled = false;
            }
        }
        //退出按钮
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //决定该文本框只能是数字或小数
        private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            string str = this.txtPrice.Text;
            e.Handled = e.KeyChar < '0' || e.KeyChar > '9';  //允许输入数字
            if (e.KeyChar == (char)8)  //允许输入回退键
            {
                e.Handled = false;
            }
            if (e.KeyChar == (char)46)
            {
                if (str == "")  //第一个不允许输入小数点
                {
                    e.Handled = true;
                    return;
                }
                else
                { //小数点不允许出现2次
                    foreach (char ch in str)
                    {
                        if (char.IsPunctuation(ch))
                        {
                            e.Handled = true;
                            return;
                        }
                    }
                    e.Handled = false;
                }
            }
        }
        //当控件成功验证时发生
        private void txtPrice_Validated(object sender, EventArgs e)
        {
           
        }
    }
}
